#include<16f877a.h>
//#device ADC=8
#include<stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void main()
{
unsigned int sensor,beatms,bpm;
while(1)
{
while(input(PIN_D1)==0);// wait for high pulse from sensor
delay_ms(50); // 10ms delay so that it does not listen to any noise
beatms = 50; // start counting beatms from 10ms since we have delay after pulse
while(input(PIN_D1)==1)// wait until signal is high
{
delay_ms(10); //wait 1msec
beatms++; //keep incrementing counter each 1ms
}
while(input(PIN_D1)==0) //keep looping till signal goes back high, wait for next
{
delay_ms(10); //wait 1msec
beatms++; //keep incrementing counter each 1ms
}
// beatms variable will now have time in ms between two high edge pulse
//lcdClear();
//lcdGotoXY(0,0);
//lcdPrint("HEART RATE : ");
bpm = 60000/beatms; // see document of #1157 for this calculation
if(bpm>=70 && bpm<=85)
{
  printf("%u\n\r",bpm);
}

else if(bpm>=86 && bpm<=90)
{
   printf("%u\n\r",bpm-20);
} 
else if(bpm>=91 && bpm<=98)
{
   printf("%u\n\r",bpm-28);
} 

else if(bpm>=100)
{


//printf ("processing"); // Display reading in BPM
}
}
}
